﻿using Hotel.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class MemberLevelDAL
    {
        private readonly Mycount db;

        public MemberLevelDAL(Mycount db)
        {
            this.db = db;
        }
        /// <summary>
        /// 会员等级的增加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int AddMemberLevel(MemberLevel info)
        {
            try
            {
                db.memberLevels.Add(info);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 判断等级名称是否重复
        /// </summary>
        /// <param name="levelName">等级名称</param>
        /// <returns></returns>
        public int SearchLevelName(string levelName)
        {
            try
            {
                int res = db.memberLevels.Where(c => c.LevelName == levelName).Count();
                return res;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取会员等级列表
        /// </summary>
        /// <returns></returns>
        public List<MemberLevel> MemberLevelsList()
        {
            try
            {
                return db.memberLevels.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取会员列表
        /// </summary>
        /// <param name="id">会员编号</param>
        /// <param name="name">会员名称</param>
        /// <param name="levelId">等级名称</param>
        /// <returns></returns>

        public List<Member> MemberList(int? id = null, string? name = "", int? levelId = null)
        {
            try
            {
                //查询所有显示的数据
                var list = db.members.Where(c => c.IsDeleted == false).ToList();
                //根据条件查询
                if (id != null)
                {
                    list = list.Where(c => c.Id == id).ToList();
                }

                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(c => c.Name.Contains(name)).ToList();
                }

                if (levelId != null)
                {
                    list = list.Where(c => c.LevelId == levelId).ToList();
                }

                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}

